package com.coin.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coin.exchange.gate.rest.stock.IStockRestApi;
import com.coin.exchange.gate.vo.DepositAddressRequestVO;
import com.coin.exchange.gate.vo.DepositWithdrawalsRequestVO;
import com.coin.exchange.gate.vo.GateBalanceRequestVO;
import com.coin.exchange.gate.vo.GateBuyRequestVO;
import com.coin.exchange.gate.vo.GateCancelAllOrdersRequestVO;
import com.coin.exchange.gate.vo.GateCancelOrderRequestVO;
import com.coin.exchange.gate.vo.GateOrderRequestVO;
import com.coin.exchange.gate.vo.GateSellRequestVO;
import com.coin.exchange.gate.vo.GateTradeHistoryRequestVO;
import com.coin.exchange.gate.vo.GateWithdrawRequestVO;
import com.coin.exchange.gate.vo.OpenOrdersRequestVO;
import com.coin.framework.bean.ResultForm;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/rest/gateio")
public class GateioController {

	private static Logger logger = LoggerFactory.getLogger(GateioController.class);

	@Autowired
	private IStockRestApi iStockRestApi;

	@ApiOperation(value = "获取所有系统支持的交易对")
	@RequestMapping(value = "/getPairs", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultForm<?> getPairs() {
		ResultForm<?> resultForm = null;
		try {
			String res = iStockRestApi.pairs();
			JSONArray resultObject = JSONArray.parseArray(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取所有系统支持的交易市场的参数信息", notes = "包括交易费，最小下单量，价格精度等")
	@RequestMapping(value = "/getMarketInfo", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultForm<?> getMarketInfo() {
		ResultForm<?> resultForm = null;
		try {
			String res = iStockRestApi.marketinfo();
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取所有系统支持的交易市场的详细行情和币种信息", notes = "包括币种名，市值，供应量，最新价格，涨跌趋势，价格曲线等")
	@RequestMapping(value = "/getMarketList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultForm<?> getMarketList() {
		ResultForm<?> resultForm = null;
		try {
			String res = iStockRestApi.marketlist();
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取系统支持的所有交易对的 最新，最高，最低 交易行情和交易量", notes = "每10秒钟更新")
	@RequestMapping(value = "/getTickers", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultForm<?> getTickers() {
		ResultForm<?> resultForm = null;
		try {
			String res = iStockRestApi.tickers();
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取单项交易对的最新，最高，最低 交易行情和交易量", notes = "每10秒钟更新")
	@RequestMapping(value = "/getTicker", method = RequestMethod.POST)
	public ResultForm<?> getTicker(@RequestBody String symbol) {
		ResultForm<?> resultForm = null;
		try {
//			String symbol = jsonObject.getString("symbol");
			String res = iStockRestApi.ticker(symbol);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取市场深度（委托挂单）", notes = "其中 asks 是委卖单, bids 是委买单")
	@RequestMapping(value = "/getOrderBook", method = RequestMethod.POST)
	public ResultForm<?> getOrderBook(@RequestBody String symbol) {
		ResultForm<?> resultForm = null;
		try {
//			String symbol = jsonObject.getString("symbol");
			String res = iStockRestApi.orderBook(symbol);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取最新80条历史成交记录")
	@RequestMapping(value = "/getTradeHistory", method = RequestMethod.POST)
	public ResultForm<?> getTradeHistory(@RequestBody String symbol) {
		ResultForm<?> resultForm = null;
		try {
//			String symbol = jsonObject.getString("symbol");
			String res = iStockRestApi.tradeHistory(symbol);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取帐号资金余额")
	@RequestMapping(value = "/getBalance", method = RequestMethod.POST)
	public ResultForm<?> getBalance(@RequestBody @Valid GateBalanceRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String res = iStockRestApi.balance(apiKey, secretKey);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取充值地址")
	@RequestMapping(value = "/getDepositAddress", method = RequestMethod.POST)
	public ResultForm<?> getDepositAddress(@RequestBody @Valid DepositAddressRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String currency = vo.getCurrency();
			String res = iStockRestApi.depositAddress(apiKey, secretKey, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取充值提现历史")
	@RequestMapping(value = "/getDepositWithdrawals", method = RequestMethod.POST)
	public ResultForm<?> getDepositWithdrawals(@RequestBody @Valid DepositWithdrawalsRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String startTime = vo.getStartTime();
			String endTime = vo.getEndTime();
			String res = iStockRestApi.depositsWithdrawals(apiKey, secretKey, startTime, endTime);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "下单交易买入")
	@RequestMapping(value = "/buy", method = RequestMethod.POST)
	public ResultForm<?> buy(@RequestBody @Valid GateBuyRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String currencyPair = vo.getCurrency_pair();
			String rate = vo.getRate();
			String amount = vo.getAmount();
			String res = iStockRestApi.buy(apiKey, secretKey, currencyPair, rate, amount);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "下单交易卖出")
	@RequestMapping(value = "/sell", method = RequestMethod.POST)
	public ResultForm<?> sell(@RequestBody @Valid GateSellRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String currencyPair = vo.getCurrency_pair();
			String rate = vo.getRate();
			String amount = vo.getAmount();
			String res = iStockRestApi.sell(apiKey, secretKey, currencyPair, rate, amount);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "取消下单")
	@RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
	public ResultForm<?> cancelOrder(@RequestBody @Valid GateCancelOrderRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String orderNumber = vo.getOrder_number();
			String currencyPair = vo.getCurrency_pair();
			String res = iStockRestApi.cancelOrder(apiKey, secretKey, orderNumber, currencyPair);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "取消所有下单")
	@RequestMapping(value = "/cancelAllOrders", method = RequestMethod.POST)
	public ResultForm<?> cancelAllOrders(@RequestBody @Valid GateCancelAllOrdersRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String type = vo.getType();
			String currencyPair = vo.getCurrency_pair();
			String res = iStockRestApi.cancelAllOrders(apiKey, secretKey, type, currencyPair);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取下单状态")
	@RequestMapping(value = "/getOrder", method = RequestMethod.POST)
	public ResultForm<?> getOrder(@RequestBody @Valid GateOrderRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String orderNumber = vo.getOrder_number();
			String currencyPair = vo.getCurrency_pair();
			String res = iStockRestApi.getOrder(apiKey, secretKey, orderNumber, currencyPair);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取我的当前挂单列表")
	@RequestMapping(value = "/getOpenOrders", method = RequestMethod.POST)
	public ResultForm<?> getOpenOrders(@RequestBody @Valid OpenOrdersRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String res = iStockRestApi.openOrders(apiKey, secretKey);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取我的24小时内成交记录")
	@RequestMapping(value = "/myTradeHistory", method = RequestMethod.POST)
	public ResultForm<?> myTradeHistory(@RequestBody @Valid GateTradeHistoryRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String orderNumber = vo.getOrder_number();
			String currencyPair = vo.getCurrency_pair();
			String res = iStockRestApi.myTradeHistory(apiKey, secretKey, currencyPair, orderNumber);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "提现")
	@RequestMapping(value = "/withdraw", method = RequestMethod.POST)
	public ResultForm<?> withdraw(@RequestBody @Valid GateWithdrawRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String currency = vo.getCurrency();
			String amount = vo.getAmount();
			String address = vo.getAddress();
			String res = iStockRestApi.withdraw(apiKey, secretKey, currency, amount, address);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}
	
}
